import pyqtgraph.opengl as gl
from pyqtgraph.Qt import QtCore, QtWidgets
from model_manager import ModelManager, ModelType

class ModelPreview(QtWidgets.QMainWindow):
    def __init__(self, model_type):
        super().__init__()
        self.setWindowTitle("3D Aircraft Models for Swarm Simulation")
        self.resize(1200, 600)
        
        # 布局
        w = QtWidgets.QWidget()
        self.setCentralWidget(w)
        layout = QtWidgets.QHBoxLayout()
        w.setLayout(layout)
        
        self.item = None

        # 为模型创建一个 View
        view = gl.GLViewWidget()
        view.opts['distance'] = 5
        view.setBackgroundColor(100, 100, 100)

        # 添加网格
        g = gl.GLGridItem()
        view.addItem(g)

        # 获取模型数据
        md = ModelManager.get_mesh_data(model_type)

        # 添加模型
        # shader='shaded' 让平面看起来有光影，这对于低多边形模型很重要
        # smooth=False 保持硬朗的低多边形风格
        face_color = (0.1, 0.4, 0.8, 0.3)
        edge_color = (0.4, 0.9, 1.0, 1.0)
        self.item = gl.GLMeshItem(
            meshdata=md, 
            smooth=False, 
            drawEdges=True, 
            edgeColor=edge_color, 
            shader='shaded', 
            color=face_color)
        self.item.setGLOptions('additive')
        view.addItem(self.item)

        layout.addWidget(view)

        # 动画定时器
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.animate)
        self.timer.start(20)

    def animate(self):
        # 让模型旋转展示
        self.item.rotate(1, 0, 0, 1) # 绕 Z 轴旋转

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)

    if len(sys.argv) > 1:
        model_type = ModelType(int(sys.argv[1]))
    else:
        model_type = ModelType.UAV_JET

    win = ModelPreview(model_type)
    win.show()
    sys.exit(app.exec())